﻿using Sidvall.IO.Data;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http;

namespace Sidvall.Web.IO.Controllers
{
    public class FolderController : ApiController
    {
        #region Public Members

        #region Create

        [HttpPost]
        [Route("api/V1/NetFileSystem/{StoreId}/Folder/Create")]
        public async Task<HttpResponseMessage> Create(string storeId, [FromBody]ItemContext criteria)
        {
            HttpResponseMessage message;

            if (criteria == null)
                throw new System.ArgumentNullException("criteria");
            try
            {
                var result = await SystemContext.Current.FolderManager.CreateFolderAsync(storeId, criteria.Path, CancellationToken.None);
                message = this.Request.CreateResponse(HttpStatusCode.OK, result);
            }
            catch (System.Exception ex)
            {
                await SystemContext.Current.ControllerManager.LogAsync(ex);
                message = SystemContext.Current.ControllerManager.GetResponseMessage(this, ex);
            }
            return message;
        }

        #endregion
        #region Delete

        [HttpPost]
        [Route("api/V1/NetFileSystem/{StoreId}/Folder/Delete")]
        public async Task<HttpResponseMessage> Delete(string storeId, [FromBody]DeleteItemContext criteria)
        {
            HttpResponseMessage message;

            if (criteria == null)
                throw new System.ArgumentNullException("criteria");
            try
            {
                var result = await SystemContext.Current.FolderManager.DeleteFolderAsync(storeId, criteria.Path, criteria.Recursive, CancellationToken.None);
                message = this.Request.CreateResponse(HttpStatusCode.OK, result);
            }
            catch (System.Exception ex)
            {
                await SystemContext.Current.ControllerManager.LogAsync(ex);
                message = SystemContext.Current.ControllerManager.GetResponseMessage(this, ex);
            }
            return message;
        }

        #endregion
        #region Exists

        [HttpPost]
        [Route("api/V1/NetFileSystem/{StoreId}/Folder/Exists")]
        public async Task<HttpResponseMessage> Exists(string storeId, [FromBody]ItemContext criteria)
        {
            HttpResponseMessage message;

            if (criteria == null)
                throw new System.ArgumentNullException("criteria");
            try
            {
                var result = await SystemContext.Current.FolderManager.FolderExistsAsync(storeId, criteria.Path, CancellationToken.None);
                message = this.Request.CreateResponse(HttpStatusCode.OK, result);
            }
            catch (System.Exception ex)
            {
                await SystemContext.Current.ControllerManager.LogAsync(ex);
                message = SystemContext.Current.ControllerManager.GetResponseMessage(this, ex);
            }
            return message;
        }

        #endregion
        #region GetItem

        [HttpPost]
        [Route("api/V1/NetFileSystem/{StoreId}/Folder/GetItem")]
        public async Task<HttpResponseMessage> GetItem(string storeId, [FromBody]GetItemContext criteria)
        {
            if (criteria == null)
                throw new System.ArgumentNullException(nameof(criteria));
            return await GetItem(storeId, criteria.LoadDetails, criteria.Path);
        }
        [HttpGet]
        [Route("api/V1/NetFileSystem/{StoreId}/Folder/GetItem/{LoadDetails}/{*path}")]
        public async Task<HttpResponseMessage> GetItem(string storeId, bool loadDetails, string path)
        {
            HttpResponseMessage message;

            try
            {
                var result = await SystemContext.Current.FolderManager.GetFolderAsync(storeId, path, loadDetails, CancellationToken.None);
                if (result != null)
                    message = this.Request.CreateResponse(HttpStatusCode.OK, result);
                else
                    message = this.Request.CreateResponse(HttpStatusCode.NoContent);
            }
            catch (System.Exception ex)
            {
                await SystemContext.Current.ControllerManager.LogAsync(ex);
                message = SystemContext.Current.ControllerManager.GetResponseMessage(this, ex);
            }
            return message;
        }

        #endregion
        #region GetItems

        [HttpPost]
        [Route("api/V1/NetFileSystem/{StoreId}/Folder/GetItems")]
        public async Task<HttpResponseMessage> GetItems(string storeId, [FromBody]GetItemsContext criteria)
        {
            HttpResponseMessage message;

            if (criteria == null)
                throw new System.ArgumentNullException("criteria");
            try
            {
                var result = await SystemContext.Current.FolderManager.GetFoldersAsync(storeId, criteria.Path, criteria.SearchPattern, criteria.SearchOption, criteria.LoadDetails, CancellationToken.None);
                if ((result != null) && (result.Any()))
                    message = this.Request.CreateResponse(HttpStatusCode.OK, result.ToArray());
                else
                    message = this.Request.CreateResponse(HttpStatusCode.NoContent);
            }
            catch (System.Exception ex)
            {
                await SystemContext.Current.ControllerManager.LogAsync(ex);
                message = SystemContext.Current.ControllerManager.GetResponseMessage(this, ex);
            }
            return message;
        }

        #endregion
        #region GetNames

        [HttpPost]
        [Route("api/V1/NetFileSystem/{StoreId}/Folder/GetNames")]
        public async Task<HttpResponseMessage> GetNames(string storeId, [FromBody]GetNamesContext criteria)
        {
            HttpResponseMessage message;

            if (criteria == null)
                throw new System.ArgumentNullException("criteria");
            try
            {
                var result = await SystemContext.Current.FolderManager.GetFolderNamesAsync(storeId, criteria.Path, criteria.SearchPattern, criteria.SearchOption, CancellationToken.None);
                if ((result != null) && (result.Any()))
                    message = this.Request.CreateResponse(HttpStatusCode.OK, result.ToArray());
                else
                    message = this.Request.CreateResponse(HttpStatusCode.NoContent);
            }
            catch (System.Exception ex)
            {
                await SystemContext.Current.ControllerManager.LogAsync(ex);
                message = SystemContext.Current.ControllerManager.GetResponseMessage(this, ex);
            }
            return message;
        }

        #endregion
        #region Move

        [HttpPost]
        [Route("api/V1/NetFileSystem/{StoreId}/Folder/Move")]
        public async Task<HttpResponseMessage> Move(string storeId, [FromBody]CopyItemContext criteria)
        {
            HttpResponseMessage message;

            if (criteria == null)
                throw new System.ArgumentNullException("criteria");
            try
            {
                var result = await SystemContext.Current.FolderManager.MoveFolderAsync(storeId, criteria.SourcePath, criteria.DestinationPath, criteria.DeleteExisting, CancellationToken.None);
                message = this.Request.CreateResponse(HttpStatusCode.OK, result);
            }
            catch (System.Exception ex)
            {
                await SystemContext.Current.ControllerManager.LogAsync(ex);
                message = SystemContext.Current.ControllerManager.GetResponseMessage(this, ex);
            }
            return message;
        }

        #endregion

        #endregion
    }
}
